有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

JavaFX启动时在JavaFX中执行应用程序启动异常

我已经创建了一个简单的javaFX程序,并通过使该项目成为maven项目将springboot添加到javaFX中。在添加SpringBoot之前,我没有收到任何错误,应用程序运行良好。 我已经在下面提供了完整的代码

波姆。xml

    <?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.2.6.RELEASE</version>
    </parent>
    <groupId>groupId</groupId>
    <artifactId>RMI-DesktopClient</artifactId>
    <version>1.0-SNAPSHOT</version>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
    </dependencies>  
</project>

AlarmsystemApplication。爪哇

    @Configuration
@SpringBootApplication
public class AlarmSystemApplication extends Application {

    private ConfigurableApplicationContext applicationContext;

    @Override
    public void init() throws Exception {
        this.applicationContext = SpringApplication.run(AlarmSystemApplication.class);

    }
    @Override
    public void stop() throws Exception {
        applicationContext.close();
    }

    public static void main(String[] args) {
        launch(args);
    }

    @Override
    public void start(Stage stage) throws Exception {
        Login.loadView(stage);
    }
}

登录。爪哇

  public class Login {
    public static void loadView(Stage stage) {

        try {
            Parent view = FXMLLoader.load(Login.class.getResource("../../../../../resources/com.ui.views/Login.fxml"));
            stage.setScene(new Scene(view));
            stage.show();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

登录。fxml

    <AnchorPane prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8.0.171" xmlns:fx="http://javafx.com/fxml/1" fx:controller="com.alarmsystem.ui.views.Login">
   <children>
      <Label layoutX="137.0" layoutY="157.0" text="Hello JavaFX">
         <font>
            <Font size="59.0" />
         </font>
      </Label>
   </children>
</AnchorPane>

我得到的错误

    Exception in Application start method
2020-04-23 22:33:12.134  INFO 7228 --- [lication Thread] j.LocalContainerEntityManagerFactoryBean : Closing JPA EntityManagerFactory for persistence unit 'default'
2020-04-23 22:33:12.136  INFO 7228 --- [lication Thread] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Shutdown initiated...
2020-04-23 22:33:12.139  INFO 7228 --- [lication Thread] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Shutdown completed.
java.lang.reflect.InvocationTargetException
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:389)
    at com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:328)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at sun.launcher.LauncherHelper$FXHelper.main(LauncherHelper.java:767)
Caused by: java.lang.RuntimeException: Exception in Application start method
    at com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:917)
    at com.sun.javafx.application.LauncherImpl.lambda$launchApplication$154(LauncherImpl.java:182)
    at java.lang.Thread.run(Thread.java:748)
Caused by: java.lang.NullPointerException: Location is required.
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3207)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3175)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3148)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3124)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3104)
    at javafx.fxml.FXMLLoader.load(FXMLLoader.java:3097)
    at com.alarmsystem.ui.views.Login.loadView(Login.java:16)
    at com.alarmsystem.ui.AlarmSystemApplication.start(AlarmSystemApplication.java:34)
    at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$161(LauncherImpl.java:863)
    at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$174(PlatformImpl.java:326)
    at com.sun.javafx.application.PlatformImpl.lambda$null$172(PlatformImpl.java:295)
    at java.security.AccessController.doPrivileged(Native Method)
    at com.sun.javafx.application.PlatformImpl.lambda$runLater$173(PlatformImpl.java:294)
    at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
    at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
    at com.sun.glass.ui.win.WinApplication.lambda$null$147(WinApplication.java:177)
    ... 1 more
Exception running application com.alarmsystem.ui.AlarmSystemApplication

Process finished with exit code 1

文件结构 enter image description here

资源路径 enter image description here


共 (1) 个答案

  1. # 1 楼答案

    我不确定这是否与春天有关;用于FXML的资源名称不是有效的资源名称(堆栈跟踪表明无法找到FXML文件是问题所在)

    具体来说,资源名称中不能有.,因此..com.ui.views都是无效的

    因此这里有两个问题:一个是资源名称不能与其中指定的“父目录”一起使用,另一个是您在resources下创建了一个文件夹(不是),其中包含非法的.字符。还要注意resources是一个文件夹,在运行时不可用

    因此,首先,在名为com.ui.views的资源下创建一个,并将FMXL放在其中。我不使用IntelliJ,所以我不确定是否有这样做的选项,但如果没有,您可以创建一个文件夹com,它的子文件夹ui,以及它的子文件夹views,并将FXML文件放置在views

    那么FXML的正确资源名是

    Parent view = FXMLLoader.load(Login.class.getResource("/com/ui/views/Login.fxml"));
    

    如果您稍微重新构造FXML文件和Login类位于同一个包中(即,在resources下创建一个com.alarmsystem.ui.views包),那么您可以这样做

    Parent view = FXMLLoader.load(Login.class.getResource("Login.fxml"));
    

    如果需要进一步排除故障,可以通过查看target/classes文件夹来查看资源部署的位置(因此可以在运行时中找到它们)